package proy4sistdistribuidos.interfaz;

import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;

import java.util.HashMap;

import proy4sistdistribuidos.interfaz.TestCamara.BtnOnClickListener;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Proy4SistDistribuidosMovilActivity extends Activity {
	
	private static final int REQUEST_PHOTO = 1;
	private static final int REQUEST_INFO_FOTO = 10;
	private String rutaCarpetaFotoTemporal = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/";
	private String nombreFotoTemporal = "fotoTemp.jpg";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
   
    
    public void accionBoton1(View view){
    	TextView t=(TextView)findViewById(R.id.texto1);
    	t.setText(new Date(System.currentTimeMillis()).toString());
    	
    	try {
			Socket elSockete = new Socket("192.168.1.254", 21);
			t.setText("Se conecto");
			elSockete.close();
			t.setText("Se desconecto");
		} catch (UnknownHostException e) {
			t.setText(e.getLocalizedMessage());
		} catch (IOException e) {
			t.setText(e.getLocalizedMessage());
		}
    	
    }
	

	public void accionBotonSalirPrincipalMenu(View view){
    	//Se corre esta rutina para forzar el cierre de la aplicacion
    	android.os.Process.killProcess(android.os.Process.myPid());
    }
    
    public void accionBotonTomarFoto(View view){
    	File folder = new File(this.rutaCarpetaFotoTemporal);
        if(!folder.exists()){
        	folder.mkdirs();

        }
        
        File file = new File(this.rutaCarpetaFotoTemporal, this.nombreFotoTemporal);
        
        Uri outputFileUri = Uri.fromFile(file);
        
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        //createButton("Tomar foto", intent, REQUEST_PHOTO);
        startActivityForResult(intent, REQUEST_PHOTO);
        
    	//Intent intentTomarFoto= new Intent(view.getContext(), TomarFotoActivity.class);
        //startActivityForResult(intentTomarFoto, 0);
    }
    
    @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode == Activity.RESULT_OK) {
			Bundle extras;
			switch(requestCode) {
				case REQUEST_PHOTO:
					File archCreado = new File(this.rutaCarpetaFotoTemporal, this.nombreFotoTemporal);
					
					//Hehe, esto renombra el archivo a lo que se le de la gana a uno
					File archDestino = new File(this.rutaCarpetaFotoTemporal, "losPollitos"+System.currentTimeMillis()+".jpg");
					archCreado.renameTo(archDestino);
					
					//extras = data.getExtras();
					String result = Uri.fromFile(archDestino).toString();//mirar como sacar url claramente
					Toast.makeText(Proy4SistDistribuidosMovilActivity.this, result, Toast.LENGTH_LONG).show();
					Intent intentTomarFoto = new Intent(this.getApplicationContext(), TomarFotoActivity.class);
					Bundle b = new Bundle();
			        b.putString("RutaFoto", archDestino.getAbsolutePath());
			        intentTomarFoto.putExtras(b);
			        
			        try{
			        	startActivityForResult(intentTomarFoto, 0);
			        }catch(Exception ex){
			        	ex.printStackTrace();
			        }
			     break;
				 case REQUEST_INFO_FOTO:
					 Uri urlImagen = data.getData();
					 Intent intentObtenerInformacionFoto = new Intent(this.getApplicationContext(), ObtenerInformacionFotoActivity.class);
					 Bundle bundleInfoParamEntrada = new Bundle();
					 bundleInfoParamEntrada.putString("urlImagen", urlImagen.toString());
					 intentObtenerInformacionFoto.putExtras(bundleInfoParamEntrada);
				     startActivityForResult(intentObtenerInformacionFoto, 2);
				 break;
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
    
    public void accionBotonObtenerInformacionFoto(View view){
    	Toast.makeText(view.getContext(), "Por favor seleccione una foto...", 4);
    	Intent obtenerFotoGaleria = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    	 startActivityForResult(obtenerFotoGaleria, REQUEST_INFO_FOTO);	
    }
    
    public void accionBotonVerFotosTomadasEnSitio(View view){
       	Intent intentVerFotosTomadasEnSitio = new Intent(view.getContext(), VerFotosTomadasEnSitioActivity.class);
    	startActivityForResult(intentVerFotosTomadasEnSitio, 3);
    }

}